package com.gack.business.service;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.gack.business.model.BackUser;
import com.gack.business.model.HelpCenter;
import com.gack.business.repository.BackUserRepository;
import com.gack.business.repository.HelpCenterRepository;
import com.gack.helper.common.abstractobj.Result;
@Service
public class HelpCenterService implements HelpCenterServiceInterface {
	
	@Autowired
	private HelpCenterRepository helpCenterRepository;
	@Autowired
	private BackUserRepository backUserRepository;
	@Autowired
	private BackLogService backLogService;
	@Transactional
	@Override
	public Result addHelpCenter(String title, String question, String content, Integer status, String uid, String id) {
		Result result = new Result();
		
		HelpCenter helpCenter = null;
		if (id != null &&! "".equals(id) ) {
			helpCenter = helpCenterRepository.findOne(id);
		} else {
			HelpCenter help = helpCenterRepository.findByTitle(title);
			if (help != null) {
				result.setKey("error");
				result.setValue("该标题已经存在");
				return result;
			}
			helpCenter = new HelpCenter();
		}
		
		helpCenter.setTitle(title);
		helpCenter.setContent(content);
		helpCenter.setQuestion(question);
		helpCenter.setCreateTime(new Date());
		helpCenter.setCreateUid(uid);
		helpCenter.setStatus(status);
		helpCenterRepository.save(helpCenter);
		result.setKey("success");
		result.setValue("保存成功");
		backLogService.addBackLog(uid, "增加一条帮助中心内容");
		return result;
	}

	@Override
	public Result findHelpCenterList(Integer page, Integer size) {
		Result result = new Result();
		Sort sort = new Sort(Sort.Direction.DESC, "createTime");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<HelpCenter> helps = helpCenterRepository.findAll(pageable);
		for (HelpCenter helpCenter : helps) {
			BackUser backUser = backUserRepository.findOne(helpCenter.getCreateUid());
			if (backUser != null) {
				helpCenter.setCreateName(backUser.getName());
			}
			
		}
		result.setKey("success");
		result.setValue(helps);
		return result;
	}

	@Override
	public Result findHelpCenter(String id) {
		Result result = new Result();
		result.setKey("success");
		result.setValue(helpCenterRepository.findOne(id));
		return result;
	}
	
	
	@Transactional
	@Override
	public Result changeStatus(Integer status, String id, String uid) {
		Result result = new Result();
		HelpCenter helpCenter = helpCenterRepository.findOne(id);
		if (helpCenter == null) {
			result.setKey("error");
			result.setValue("查无此数据");
			return result;
		}
		if (helpCenter.getStatus() == status) {
			result.setKey("error");
			result.setValue("状态不变");
			return result;
		}
		helpCenter.setStatus(status);
		helpCenterRepository.save(helpCenter);
		result.setKey("success");
		result.setValue("更改成功");
		backLogService.addBackLog(uid, "更改了帮助中心id为 " + id +" 的状态为 " +status);
		return result;
	}

	@Transactional
	@Override
	public Result deleteHelpCenter(String id, String uid) {
		helpCenterRepository.delete(id);
		Result result = new Result();
		result.setKey("success");
		result.setValue("删除成功");
		backLogService.addBackLog(uid, "删除了帮助中心id为" + uid);
		return result;
	}

	@Override
	public Result getAllHelp() {
		List<HelpCenter> helpCenters = helpCenterRepository.findByStatus(1);
		Result result = new Result();
		if (helpCenters != null && helpCenters.size() >0) {
			result.setKey("success");
			result.setValue(helpCenters);
			return result;
		}
		result.setKey("error");
		result.setValue(null);
		
		return result;
	}

}
